<template>
  <div class="License">
    <div class="page-head">
      <div class="page-bread">
        <div class="bread-prev" @click="back">
          资源管理
        </div>
        <div class="bread-divider">
          /
        </div>
        <div class="bread-now">
          类别管理
        </div>
      </div>
      <div class="page-tool">
        <div class="tool-btn" @click="open('add')">新增类别</div>
      </div>
      
    </div>
    <div class="page-body">
      <div class="table_data">
        <el-scrollbar class="scroll-bar">
          <el-table :data="tableData" stripe :fit="true">
            <el-table-column
             :show-overflow-tooltip="true"
              prop="name"
              label="类别名称"
            >
            </el-table-column>
            <el-table-column
              prop="suffix"
              :show-overflow-tooltip="true"
              label="可用后缀"
            >
            </el-table-column>
             <el-table-column
              prop="size"
              :show-overflow-tooltip="true"
              label="大小限制"
            >
            </el-table-column>
            <el-table-column
              prop="remark"
              :show-overflow-tooltip="true"
              label="备注"
            >
            </el-table-column>
            <el-table-column label="操作" width="160" align="center">
              <template slot-scope="{row}">
                <div class="operation-col">
                  <div class="txt-btn primary" @click="open('edit', row)">修改</div>
                  <el-divider direction="vertical"></el-divider>
                  <div class="txt-btn primary" @click="doDelete(row)">删除</div>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </el-scrollbar>
      </div>
      <div class="page-footer">
        <pagina-tion
          class="page-pager"
          v-show="total>=0"
          :background="true"
          :total="total"
          :offset.sync="offset"
          :limit.sync="limit"
          @pagination="getChangePage"
        />
       <button class="page-button">确定</button>
      </div>
    </div>
    <!-- 新增和修改弹框 -->
    <el-dialog :visible.sync="model.visible" width="450px" :title="model.type==='add'?'新增类别':'修改类别'" :close-on-click-modal="false" :show-close="false">
      <div class="dialog-body">
        <el-form ref="form" :model="form" label-width="120px" label-position="left">
          <el-form-item label="类别名称" class="required">
            <el-input
              placeholder="请输入类别名称"
              :maxlength="6"
              v-model="form.name"
              name="cateName"
              v-validate="'required'"
            />
            <div v-show="errors.has('cateName')" class="el-form-item__error">{{ errors.first('cateName') }}</div>
          </el-form-item>
          <el-form-item label="可用后缀" class="required">
            <el-input
              placeholder="以英文逗号分隔，如（mp3,avi）"
              v-model="form.suffix"
              name="suffix"
              v-validate="'required|suffix|maxLength128'"
            />
            <div v-show="errors.has('suffix')" class="el-form-item__error">{{ errors.first('suffix') }}</div>
          </el-form-item>
          <el-form-item label="大小限制（M）" class="required">
            <el-input
              placeholder="请输入大小限制"
              v-model="form.size"
              name="size"
              type="text"
              v-validate="'required|size'"
            />
            <div v-show="errors.has('size')" class="el-form-item__error">{{ errors.first('size') }}</div>
          </el-form-item>
          <el-form-item label="备注">
            <el-input
              type="textarea"
              maxlength="128"
              v-model.trim="form.remark"
            />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="close">取 消</el-button>
        <el-button type="primary" @click="submit(model.type)">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog :visible.sync="stopVisable" width="450px" title="终止 License" :close-on-click-modal="false" :show-close="false">
      <div class="dialog-body">
        <el-form ref="form" :model="form" label-width="90px" label-position="left">
          <el-form-item label="终止原因">
            <el-input
              type="textarea"
              placeholder="请输入终止原因"
            />
          </el-form-item>
          
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="stopVisable = false">取消</el-button>
        <el-button type="primary" @click="submit(model.type)">终止续约</el-button>
      </span>
    </el-dialog>

      <!-- 删除弹窗 -->
    <el-dialog
      :visible.sync="visibility"
      width="430px"
      height="100px"
      :show-close="false"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <div class="dialog-content comfirm-body">
        <img class="comfirm-ico"
             src="~assets/img/warning.png">
        <div class="comfirm-txt">
          <div>
            确定删除该条数据？
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="visibility = false">取 消</el-button>
        <el-button type="primary" @click="resTypeDelete">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { limit } from '@/consts/consts'
import * as api from '@/api'
import { resTypeFormKey } from '@/consts/formKey'
export default {
  data() {
    return {
      limit,
      total: 0,
      offset: 0,
      tableData: [],
      model: {
        type: 'add',
        visible: false
      },
      stopVisable: false,
      form: {
        ...resTypeFormKey
      },
      type_id:'',
      uploadFile: null,
      visibility: false
    }
  },
  methods: {
    back() {
      this.$router.go(-1)
    },
    close() {
      this.model.visible = false
      this.form = {...resTypeFormKey}
    },

    open(type, row=null) {
      this.$validator.reset()
      this.model.type = type
      if (row) {
        for (const k in resTypeFormKey) {
          this.form[k] = row[k]
        }

        this.type_id = row.type_id
      }
      this.model.visible = true
    },
    getChangePage(val) {
      this.offset = val.offset
      this.getList()
    },

    submit() {
      this.$validator.validateAll().then(valid => {
        if (valid) {
          this.addOrEdit()
        } else {
          this.$message.warning('有信息未填写正确')
        }
      })
    },
         
    doDelete(row) { 
      this.type_id = row.type_id
      this.visibility = true
    },

    resTypeDelete(){
      this.typeDelete()
    },
    // 应用分组
    async getList() {
      const { code, data } = await api.category(this).resTypeList({
        offset: this.offset,
        limit:  this.limit
      })
      if (code === 0) {
        this.tableData = data
      }
    },  
    async addOrEdit() {
      let fn = this.model.type === 'add' ? 'resTypeAdd' : 'resTypeUpdate'
      const { code } = await api.category(this)[fn](this.form, this.type_id)
      if (code === 0) {
        this.form.name = ''
        this.form.remark = ''
        this.form.cover_url = ''
        this.$message.success('操作成功')
        this.close()
        this.getList()
      }
    },
    
    async typeDelete() {
      const { code } = await api.category(this).resTypeDelete(this.type_id)
      if (code === 0) {
        this.$message.success('操作成功')
        this.program_id = ''
        this.visibility = false
        if (this.total % this.limit === 1) {
          if (this.offset >= this.limit) {
            this.offset -= this.limit
          }
        }
        this.getList()
      }
    },
  },
  created(){
    this.getList()
  },
}
</script>

<style lang="stylus">
.Type {

}
</style>